package com.yuyang.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yuyang.common.utils.PageUtils;
import com.yuyang.common.utils.Query;
import com.yuyang.gulimall.product.dao.BrandDao;
import com.yuyang.gulimall.product.dao.CategoryDao;
import com.yuyang.gulimall.product.entity.BrandEntity;
import com.yuyang.gulimall.product.entity.CategoryEntity;
import com.yuyang.gulimall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.yuyang.gulimall.product.dao.CategoryBrandRelationDao;
import com.yuyang.gulimall.product.entity.CategoryBrandRelationEntity;
import com.yuyang.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    BrandDao brandDao;
    @Autowired
    CategoryDao categoryDao;

    @Autowired
    BrandService brandService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void updateBrand(Long brandId, String brandName) {
        this.update(new LambdaUpdateWrapper<CategoryBrandRelationEntity>().
                eq(CategoryBrandRelationEntity::getBrandId,brandId).
                set(CategoryBrandRelationEntity::getBrandName,brandName));
    }

    @Override
    public void updateCategory(Long catId, String categoryName) {
        LambdaUpdateWrapper<CategoryBrandRelationEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<CategoryBrandRelationEntity>()
                .eq(CategoryBrandRelationEntity::getCatelogId, catId)
                .set(CategoryBrandRelationEntity::getCatelogName, categoryName);
        this.update(lambdaUpdateWrapper);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();

        long count = this.count(new LambdaUpdateWrapper<CategoryBrandRelationEntity>().
                eq(CategoryBrandRelationEntity::getBrandId, brandId).
                eq(CategoryBrandRelationEntity::getCatelogId, catelogId));
        //避免重复关联
        if (count == 0){
            BrandEntity brandEntity = brandDao.selectById(brandId);
            categoryBrandRelation.setBrandName(brandEntity.getName());
            CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
            categoryBrandRelation.setCatelogName(categoryEntity.getName());
            this.save(categoryBrandRelation);
        }
    }

    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> relationEntities= this.list(new LambdaUpdateWrapper<CategoryBrandRelationEntity>().eq(CategoryBrandRelationEntity::getCatelogId, catId));
        List<BrandEntity> brandEntities = relationEntities.stream().map(relationEntity -> brandService.getById(relationEntity.getBrandId())).collect(Collectors.toList());

        return brandEntities;
    }
}